package org.duiduo.util;

import com.alibaba.fastjson.JSONObject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;


    public void zadd(String key, Object value, double score) {
        redisTemplate.opsForZSet().add(key, value, score);
    }

    public Set<Object> zrangeByScore(String key, double min, double max, long offset, long count) {
        return redisTemplate.opsForZSet().rangeByScore(key, min, max, offset, count);
    }

    public Long zremove(String key, Object value) {
        return redisTemplate.opsForZSet().remove(key, value);
    }


    public boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    public Collection<String> keys(String pattern) {
        return redisTemplate.keys(pattern);
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }

    public void delete(Collection<String> key) {
        redisTemplate.delete(key);
    }

    public void setByString(String key, String value) {
        if (value == null) {
            return;
        }
        stringRedisTemplate.opsForValue().set(key, value);
    }

    public void setByString(String key, String value, Long timeout, TimeUnit unit) {
        if (value == null) {
            return;
        }
        stringRedisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    public Object get(String key) throws Exception{
        Object value = redisTemplate.opsForValue().get(key);
        return value;
    }

    public <T> T getJavaObject(String key, Class<T> clazz) {
        Object value = redisTemplate.opsForValue().get(key);
        return JSONObject.parseObject(JSONObject.toJSONString(value), clazz);
    }

    public String getByString(String key) throws Exception{
        return stringRedisTemplate.opsForValue().get(key);
    }

    public void set(String key, Object obj) {
        if (obj == null) {
            return;
        }
        redisTemplate.opsForValue().set(key, obj);
    }

    public void set(String key, Object obj, Long timeout, TimeUnit unit) {
        if (obj == null) {
            return;
        }

        if (timeout != null && timeout != 0l) {
            redisTemplate.opsForValue().set(key, obj, timeout, unit);
        } else {
            redisTemplate.opsForValue().set(key, obj);
        }
    }

    public boolean setIfAbsent(String key, Object value, long timeout) {
        boolean absentFlage = redisTemplate.opsForValue().setIfAbsent(key, value);
        if(absentFlage){
            boolean expireFlage=redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
            if(!expireFlage){
                redisTemplate.delete(key);
                return false;
            }
            return true;
        }
        return false;
    }
}
